********************************************************************************
** 	TITLE:  c4_robustness_checks.do
**
**	PROJECT: IGNITE
** 
**  PURPOSE: Table IV: Robustness Checks

********************************************************************************
********************************************************************************
		set sortseed 13

use "$output_data/estimate.dta", clear

***************************************************************************
*Row 1. Baseline Specification
***************************************************************************
	local name_r1 = "Baseline Specification"
	foreach y in num_maj_weekly return_3m{
		
			//stay ignite_0 for misconduct
			if "`y'" == "num_maj_weekly"{
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc
			}
			//stay ignite_0 for Recidivism
			if "`y'" == "return_3m"{
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
			}
		
		qui ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
			
		qui ereturn display
		mat R = r(table)
		
		local `y'ig_pc: di %8.3f _b[ignite_0]
		local `y'ig_pcse: di %8.3f _se[ignite_0]
							
			
		local p_val =  el(R,4,1)
		if `p_val'<0.01 {
			local star="***"
		}
		if `p_val'>=0.01 & `p_val'<0.05 {
			local star="**"
		}
		if `p_val'>=0.05 & `p_val'<0.1 {
			local star="*"
		}
		if `p_val'>=0.1 {
			local star=""
		}
							
		local `y'_r1 = "``y'ig_pc'" +"`star'"
		local `y'se_r1 = subinstr("("+"``y'ig_pcse'"+")"," ","",.)
		local `y'obs_r1: di %8.0gc e(N)
	}

***************************************************************************
*Row 2. High Predicted Risk Sample
***************************************************************************
	local name_r2 = "High Predicted Risk Sample"
	preserve
	use "$output_data/prediction.dta",clear
	
* High predicted risk sample by quartiles
	cap drop risk_tile*
	local v return_3m
	xtile risk_tile = `v'_hat if booking_year>=2016 , n(4)
	keep if risk_tile==4
		
	foreach y in num_maj_weekly return_3m{
		
			//stay ignite_0 for misconduct
			if "`y'" == "num_maj_weekly"{
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc
			}
			//stay ignite_0 for Recidivism
			if "`y'" == "return_3m"{
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
			}
		
		 ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
			
		qui ereturn display
		mat R = r(table)
		
		local `y'ig_pc: di %8.3f _b[ignite_0]
		local `y'ig_pcse: di %8.3f _se[ignite_0]
							
			
		local p_val =  el(R,4,1)
		if `p_val'<0.01 {
			local star="***"
		}
		if `p_val'>=0.01 & `p_val'<0.05 {
			local star="**"
		}
		if `p_val'>=0.05 & `p_val'<0.1 {
			local star="*"
		}
		if `p_val'>=0.1 {
			local star=""
		}
							
		local `y'_r2 = "``y'ig_pc'" +"`star'"
		local `y'se_r2 = subinstr("("+"``y'ig_pcse'"+")"," ","",.)
		local `y'obs_r2: di %8.0gc e(N)
	}

restore

***************************************************************************
*Row 3. Misconduct not Involving Others
***************************************************************************
	local name_r3 = "Misconduct not Involving Others"
	preserve
	//stay ignite_0 for misconduct
	cap drop stay
	gen stay = stay_misc
	cap drop ignite_0
	gen ignite_0 = ignite_0_misc
				
	drop num_maj_weekly
	gen num_maj_weekly = num_maj_not_other/(30*stay/7) 
	replace num_maj_weekly=0 if num_maj_not_other==0
	
	//replacing missing num_maj_not_other values as 0
	replace num_maj_weekly=0 if num_maj_not_other==.
	replace num_maj_weekly= 0 if num_maj_weekly ==. 

	
	foreach y in num_maj_weekly {
		qui ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
			
		qui ereturn display
		mat R = r(table)
		
		local `y'ig_pc: di %8.3f _b[ignite_0]
		local `y'ig_pcse: di %8.3f _se[ignite_0]
							
			
		local p_val =  el(R,4,1)
		if `p_val'<0.01 {
			local star="***"
		}
		if `p_val'>=0.01 & `p_val'<0.05 {
			local star="**"
		}
		if `p_val'>=0.05 & `p_val'<0.1 {
			local star="*"
		}
		if `p_val'>=0.1 {
			local star=""
		}
							
		local `y'_r3 = "``y'ig_pc'" +"`star'"
		local `y'se_r3 = subinstr("("+"``y'ig_pcse'"+")"," ","",.)
		local `y'obs_r3: di %8.0gc e(N)
	}
	restore

***************************************************************************
*Row 4. Jail Crowding
***************************************************************************
preserve
******************************************
***** Prepare Data -- Jail Crowding
******************************************
use "$output_data/prediction.dta", clear

keep inmate booking_date release_date

egen ID = group(inmate booking_date)

format %td booking_date

gen duration = release_date - booking_date + 1
drop if missing(duration)
expand duration

bysort ID: gen jail_date = booking_date + _n-1
format  %td jail_date

gen population = 1
collapse (sum) population, by(jail_date)
keep if inrange(jail_date, td(01jan2016), td(31may2022))
codebook population

rename population jail_crowding
sum jail_crowding, meanonly
gen demean_jail_crowd = jail_crowding - `r(mean)'
gen booking_date = jail_date
keep booking_date jail_date demean_jail_crowd // 
tempfile jail_crowding
save `jail_crowding'
restore 
******************************************
***** Merge Jail Crowding Measure with Data and perform analysis
******************************************

	local name_r4 = "Jail Crowding $ \times $ Delay Control"
preserve

merge m:1 booking_date using `jail_crowding', nogen keep(1 3)

gen interact_delay_crowd = D_remove_any*demean_jail_crowd

	
	foreach y in num_maj_weekly return_3m{
			//stay ignite_0 for misconduct
			if "`y'" == "num_maj_weekly"{
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc
			}
			//stay ignite_0 for Recidivism
			if "`y'" == "return_3m"{
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
			}
	quiet ivreghdfe `y' (ignite_0 stay = D_remove_any interact_proxy_any) ///
	$auxiliary_control  interact_delay_crowd demean_jail_crowd D_ignite_proxy ///
	if `y'>=0, ///
	cluster(inmate) first a($design_control)

		qui ereturn display
		mat R = r(table)
		
		local `y'ig_pc: di %8.3f _b[ignite_0]
		local `y'ig_pcse: di %8.3f _se[ignite_0]
							
			
		local p_val =  el(R,4,1)
		if `p_val'<0.01 {
			local star="***"
		}
		if `p_val'>=0.01 & `p_val'<0.05 {
			local star="**"
		}
		if `p_val'>=0.05 & `p_val'<0.1 {
			local star="*"
		}
		if `p_val'>=0.1 {
			local star=""
		}
							
		local `y'_r4 = "``y'ig_pc'" +"`star'"
		local `y'se_r4 = subinstr("("+"``y'ig_pcse'"+")"," ","",.)
		local `y'obs_r4: di %8.0gc e(N)
	}
restore
	
***************************************************************************
*Row 5. Time Trend × Delay Control
***************************************************************************
	local name_r5 = "Time Trend $ \times $ Delay Control"
	preserve
		

***** ym variable is normalized to zero at August 2020, one month before start of IGNITE.
//interaction of the time trend with a post-IGNITE as well as the interaction of 
	//the time_delay variable with a post-IGNITE dummy	

gen ym_booked_norm = ym_booked - 727
	gen time_delay = D_remove_any*ym_booked_norm
	gen time_delay_int = time_delay*D_ignite_proxy  

//IV Regression
	foreach y in num_maj_weekly return_3m{
			//stay ignite_0 for misconduct
			if "`y'" == "num_maj_weekly"{
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc
			}
			//stay ignite_0 for Recidivism
			if "`y'" == "return_3m"{
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
			}
		qui ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any ) time_delay ym_booked_norm D_ignite_proxy time_delay_int ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
			
		qui ereturn display
		mat R = r(table)
		
		local `y'ig_pc: di %8.3f _b[ignite_0]
		local `y'ig_pcse: di %8.3f _se[ignite_0]
							
			
		local p_val =  el(R,4,1)
		if `p_val'<0.01 {
			local star="***"
		}
		if `p_val'>=0.01 & `p_val'<0.05 {
			local star="**"
		}
		if `p_val'>=0.05 & `p_val'<0.1 {
			local star="*"
		}
		if `p_val'>=0.1 {
			local star=""
		}
							
		local `y'_r5 = "``y'ig_pc'" +"`star'"
		local `y'se_r5 = subinstr("("+"``y'ig_pcse'"+")"," ","",.)
		local `y'obs_r5: di %8.0gc e(N)
	}
	restore

***************************************************************************
*Row 6. Excluding COVID Period
***************************************************************************
	local name_r6 = "Excluding COVID Period"
	preserve
	drop if ym_booked >= mofd(date("03/01/2020","MDY"))& ///
		ym_booked < mofd(date("06/01/2021","MDY"))

	foreach y in num_maj_weekly return_3m{
			//stay ignite_0 for misconduct
			if "`y'" == "num_maj_weekly"{
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc
			}
			//stay ignite_0 for Recidivism
			if "`y'" == "return_3m"{
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
			}
		qui ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
		qui ereturn display
		mat R = r(table)
		
		local `y'ig_pc: di %8.3f _b[ignite_0]
		local `y'ig_pcse: di %8.3f _se[ignite_0]
							
			
		local p_val =  el(R,4,1)
		if `p_val'<0.01 {
			local star="***"
		}
		if `p_val'>=0.01 & `p_val'<0.05 {
			local star="**"
		}
		if `p_val'>=0.05 & `p_val'<0.1 {
			local star="*"
		}
		if `p_val'>=0.1 {
			local star=""
		}
							
		local `y'_r6 = "``y'ig_pc'" +"`star'"
		local `y'se_r6 = subinstr("("+"``y'ig_pcse'"+")"," ","",.)
		local `y'obs_r6: di %8.0gc e(N)
	}

	restore


***************************************************************************
*Row 7. Including Circuit Court Delay
***************************************************************************
	
	local name_r7 = "Including Circuit Court Delay"
	preserve
***** bring in circuit data
	merge 1:1 inmate booking_date using "$output_data/circuit_delays_20240815.dta", nogen keep(1 3)
	egen D_remove_all = rowmax(D_remove_any D_remove_any_circ)
	gen interact_proxy_all = D_ignite_proxy*D_remove_all

	foreach y in num_maj_weekly return_3m{
			//stay ignite_0 for misconduct
			if "`y'" == "num_maj_weekly"{
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc
			}
			//stay ignite_0 for Recidivism
			if "`y'" == "return_3m"{
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
			}
		qui ivreghdfe `y' (ignite_0 stay=D_remove_all interact_proxy_all) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
		
		ereturn display
		mat R = r(table)
		
		local `y'ig_cc: di %8.3f _b[ignite_0]
		local `y'ig_ccse: di %8.3f _se[ignite_0]
							
			
		local p_val =  el(R,4,1)
		if `p_val'<0.01 {
			local star="***"
		}
		if `p_val'>=0.01 & `p_val'<0.05 {
			local star="**"
		}
		if `p_val'>=0.05 & `p_val'<0.1 {
			local star="*"
		}
		if `p_val'>=0.1 {
			local star=""
		}
							
		local `y'_r7 = "``y'ig_cc'" +"`star'"
		local `y'se_r7 = subinstr("("+"``y'ig_ccse'"+")"," ","",.)
		local `y'obs_r7: di %8.0gc e(N)			
	}

	restore

***************************************************************************
*Row 8. Fiscal Crisis / Close to Holidays Delays
***************************************************************************

	local name_r8 = "Fiscal Crisis / Holiday Delays Only"

	preserve
	//Fiscal Crises Delays
	merge 1:m inmate booking_date using "$output_data/crises_estimates_cases.dta", nogen keep(1 3) assert(1 3)

	//Holiday Delays [2 weeks band]
	merge 1:1 inmate booking_date using "$output_data/holiday_delays_2w.dta", keep(1 3) nogen

***** Change definition of a delay 
	//Fiscal Crises period
	gen D_fiscal = ym_booked>=mofd(date("06/01/2020","MDY"))&ym_booked<mofd(date("08/01/2020","MDY"))	

	//gen remove instrument equals the original removal instrument
	gen D_remove_noncovquasi = D_remove_any 
	//Set removals that occurred outside of holiday band and fiscal closure to equal zero
		//days around holiday (14 days or two weeks)
	replace D_remove_noncovquasi=0 if near_hld_14d!=1 & D_fiscal_resch != 1
	
	gen interact_proxy_noncovquasi = D_remove_noncovquasi*D_ignite_proxy

//IV Regression	
	foreach y in num_maj_weekly return_3m{
			//stay ignite_0 for misconduct
			if "`y'" == "num_maj_weekly"{
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc
			}
			//stay ignite_0 for Recidivism
			if "`y'" == "return_3m"{
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
			}
		
		 ivreghdfe `y' (ignite_0 stay=D_remove_noncovquasi interact_proxy_noncovquasi ) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
		
		ereturn display
		mat R = r(table)
		
		local `y'ig_nc: di %8.3f _b[ignite_0]
		local `y'ig_ncse: di %8.3f _se[ignite_0]
							
			
		local p_val =  el(R,4,1)
		if `p_val'<0.01 {
			local star="***"
		}
		if `p_val'>=0.01 & `p_val'<0.05 {
			local star="**"
		}
		if `p_val'>=0.05 & `p_val'<0.1 {
			local star="*"
		}
		if `p_val'>=0.1 {
			local star=""
		}
							
		local `y'_r8 = "``y'ig_nc'" +"`star'"
		local `y'se_r8 = subinstr("("+"``y'ig_ncse'"+")"," ","",.)
		local `y'obs_r8: di %8.0gc e(N)			
	}

	restore
	
***************************************************************************
*Row 9. Multiple Delays per Day
***************************************************************************
	local name_r9 = "Multiple Delays per Day"
	preserve

	local j=3
	cap gen interact_proxy_`j'pd=D_remove_`j'pd*D_ignite_proxy
	foreach y in num_maj_weekly return_3m{
			//stay ignite_0 for misconduct
			if "`y'" == "num_maj_weekly"{
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc
			}
			//stay ignite_0 for Recidivism
			if "`y'" == "return_3m"{
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
			}
		
		qui ivreghdfe `y' (ignite_0 stay=D_remove_`j'pd interact_proxy_`j'pd) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
		
		ereturn display
		mat R = r(table)
		
		local `y'ig_`j'pd: di %8.3f _b[ignite_0]
		local `y'ig_`j'pdse: di %8.3f _se[ignite_0]
							
			
		local p_val =  el(R,4,1)
		if `p_val'<0.01 {
			local star="***"
		}
		if `p_val'>=0.01 & `p_val'<0.05 {
			local star="**"
		}
		if `p_val'>=0.05 & `p_val'<0.1 {
			local star="*"
		}
		if `p_val'>=0.1 {
			local star=""
		}
							
		local `y'_r9 = "``y'ig_`j'pd'" +"`star'"
		local `y'se_r9 = subinstr("("+"``y'ig_`j'pdse'"+")"," ","",.)
		local `y'obs_r9: di %8.0gc e(N)			
	}


	restore

***************************************************************************
*Row 10. Multiple Delay Events Control
***************************************************************************
	local name_r10 = "Multiple Delay Events Control"
	preserve

	egen num_delays = rowtotal(D_remove_event*)
	gen multiple = num_delays>=2

	foreach y in num_maj_weekly return_3m{
			//stay ignite_0 for misconduct
			if "`y'" == "num_maj_weekly"{
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc
			}
			//stay ignite_0 for Recidivism
			if "`y'" == "return_3m"{
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
			}
		qui ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any) D_ignite_proxy ///
			multiple $auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
			
		qui ereturn display
		mat R = r(table)
		
		local `y'ig_pc: di %8.3f _b[ignite_0]
		local `y'ig_pcse: di %8.3f _se[ignite_0]
							
			
		local p_val =  el(R,4,1)
		if `p_val'<0.01 {
			local star="***"
		}
		if `p_val'>=0.01 & `p_val'<0.05 {
			local star="**"
		}
		if `p_val'>=0.05 & `p_val'<0.1 {
			local star="*"
		}
		if `p_val'>=0.1 {
			local star=""
		}
							
		local `y'_r10 = "``y'ig_pc'" +"`star'"
		local `y'se_r10 = subinstr("("+"``y'ig_pcse'"+")"," ","",.)
		local `y'obs_r10 : di %8.0gc e(N)
	}


	restore

***************************************************************************
*Row 11. Non-IGNITE Hours Misconduct
***************************************************************************
	local name_r11 = "Non-IGNITE Hours Misconduct"
	preserve
			//stay ignite_0 for misconduct
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc
		
	drop num_maj_weekly
	gen num_maj_weekly = num_maj_not_ignite_hr/(30*stay/7) 
	replace num_maj_weekly = 0 if mi(num_maj_weekly)
	replace num_maj_weekly=0 if num_maj_not_ignite_hr==0
	foreach y in num_maj_weekly {
		qui ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
			
		qui ereturn display
		mat R = r(table)
		
		local `y'ig_pc: di %8.3f _b[ignite_0]
		local `y'ig_pcse: di %8.3f _se[ignite_0]
							
			
		local p_val =  el(R,4,1)
		if `p_val'<0.01 {
			local star="***"
		}
		if `p_val'>=0.01 & `p_val'<0.05 {
			local star="**"
		}
		if `p_val'>=0.05 & `p_val'<0.1 {
			local star="*"
		}
		if `p_val'>=0.1 {
			local star=""
		}
							
		local `y'_r11 = "``y'ig_pc'" +"`star'"
		local `y'se_r11 = subinstr("("+"``y'ig_pcse'"+")"," ","",.)
		local `y'obs_r11: di %8.0gc e(N)
	}
	restore


***************************************************************************
*Row 12. Non-Tablet Hours Misconduct
***************************************************************************
	local name_r12 = "Non-Tablet Hours Misconduct"
	preserve
			//stay ignite_0 for misconduct
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc
				
	drop num_maj_weekly
	gen num_maj_weekly = num_maj_not_tablet_hr/(30*stay/7)
	replace num_maj_weekly = 0 if mi(num_maj_weekly)
	replace num_maj_weekly=0 if num_maj_not_tablet_hr==0
	foreach y in num_maj_weekly {
		qui ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
			
		qui ereturn display
		mat R = r(table)
		
		local `y'ig_pc: di %8.3f _b[ignite_0]
		local `y'ig_pcse: di %8.3f _se[ignite_0]
							
			
		local p_val =  el(R,4,1)
		if `p_val'<0.01 {
			local star="***"
		}
		if `p_val'>=0.01 & `p_val'<0.05 {
			local star="**"
		}
		if `p_val'>=0.05 & `p_val'<0.1 {
			local star="*"
		}
		if `p_val'>=0.1 {
			local star=""
		}
							
		local `y'_r12 = "``y'ig_pc'" +"`star'"
		local `y'se_r12 = subinstr("("+"``y'ig_pcse'"+")"," ","",.)
		local `y'obs_r12: di %8.0gc e(N)
	}
	restore

***************************************************************************
*Make Table 4
***************************************************************************
	
	cap file close fh
	cap erase "$tables/table4_robust.tex"
	file open fh using "$tables/table4_robust.tex", write replace
	file write fh ///
			"\vspace{-.4cm} \\ "_n
			
	forval j=1/12{
		file write fh ///
		"`name_r`j'' & `num_maj_weekly_r`j'' & `return_3m_r`j'' \\ "_n ///
		" ($ N = `num_maj_weeklyobs_r`j'' $) & `num_maj_weeklyse_r`j'' & `return_3mse_r`j'' \\ "_n ///
		"\vspace{-.2cm}\\" _n
	}

	cap file close fh
	macro drop fh
